home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !FednetCmp / c / Utils < prev   
Encoding:
Text File  |  2003-10-16  |  6.3 KB  |  216 lines

  1. /*
  2.  *  FednetCmp - Fednet file compression/decompression
  3.  *  Utility functions
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26.  
  27. /* RISC OS library files */
  28. #include "kernel.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "window.h"
  32. #include "saveas.h"
  33. #include "wimplib.h"
  34. #include "gadgets.h"
  35. #include "swis.h"
  36. #include "flex.h"
  37.  
  38. /* My library files */
  39. #include "err.h"
  40. #include "msgtrans.h"
  41. #include "hourglass.h"
  42. #include "Macros.h"
  43. #include "FilePerc.h"
  44.  
  45. /* Local headers */
  46. #include "Utils.h"
  47. #include "Main.h"
  48.  
  49. /* ----------------------------------------------------------------------- */
  50. /*                         Public functions                                */
  51.  
  52. _kernel_oserror *open_above_iconbar(unsigned int flags, ObjectId id, ObjectId parent, ComponentId parent_component)
  53. {
  54.   /* Open window horizontally centred on pointer, above iconbar */
  55.   WimpGetPointerInfoBlock pointerinfo;
  56.   WindowShowObjectBlock showblock;
  57.   WimpGetWindowStateBlock winstate;
  58.   ObjectId win_id;
  59.   
  60.   THROW(wimp_get_pointer_info(&pointerinfo));
  61.   THROW(saveas_get_window_id(0, id, &win_id));
  62.   THROW(window_get_wimp_handle(0, win_id, &(winstate.window_handle)));
  63.   THROW(wimp_get_window_state(&winstate));
  64.  
  65.   showblock.visible_area.xmin = pointerinfo.x - (winstate.visible_area.xmax - winstate.visible_area.xmin)/2;
  66.   showblock.visible_area.ymin = 96 + (winstate.visible_area.ymax - winstate.visible_area.ymin);
  67.   return toolbox_show_object(flags, id, Toolbox_ShowObject_TopLeft, &showblock, parent, parent_component);
  68. }
  69.  
  70. /* ----------------------------------------------------------------------- */
  71.  
  72. char *copystring(char *string)
  73. {
  74.   /* copy string into newly allocated memory */
  75.   char *ptr;
  76.  
  77.   ptr = malloc(strlen(string)+1);
  78.   if(ptr == NULL) 
  79.     err_complain(0, msgs_global("NoMem"));
  80.   else
  81.     strcpy(ptr, string);
  82.   return ptr;
  83. }
  84.  
  85.  
  86. /* ----------------------------------------------------------------------- */
  87.  
  88. _kernel_oserror *showgadget(ObjectId window, ComponentId gadget)
  89. {
  90.   int height;
  91.   BBox pos;
  92.   THROW(gadget_get_bbox(0, window, gadget, &pos));
  93.   if(pos.ymin < 0) /* already shown */
  94.     return NULL;
  95.   /* shift the icon into the window to show the button */
  96.   height = pos.ymax - pos.ymin;
  97.   pos.ymin = -pos.ymin; /* implicitly preserves dist. icbottom-to-wintop */
  98.   pos.ymax = pos.ymin+height; /* keep dist. icbottom-to-ictop */
  99.   return gadget_move_gadget(0, window, gadget, &pos);
  100. }
  101.  
  102. /* ----------------------------------------------------------------------- */
  103.  
  104. _kernel_oserror *hidegadget(ObjectId window, ComponentId gadget)
  105. {
  106.   int height;
  107.   BBox pos;
  108.   THROW(gadget_get_bbox(0, window, gadget, &pos));
  109.   if(pos.ymin > 0) /* already hidden */
  110.     return NULL;
  111.   /* shift the icon off the top to show the button */
  112.   height = pos.ymax - pos.ymin;
  113.   pos.ymin = -pos.ymin; /* implicitly preserves dist. icbottom-to-wintop */
  114.   pos.ymax = pos.ymin+height; /* keep dist. icbottom-to-ictop */
  115.   return gadget_move_gadget(0, window, gadget, &pos);  
  116. }
  117.  
  118. /* ----------------------------------------------------------------------- */
  119.  
  120. _kernel_oserror *set_gadget_faded(bool fade, ObjectId id, ComponentId gad)
  121. {
  122.   unsigned int flags_settings;
  123.  
  124.   THROW(gadget_get_flags(0, id, gad, &flags_settings));
  125.   if(fade) {
  126.     if(FLAG_SET(flags_settings, Gadget_Faded))
  127.       return NULL;
  128.     flags_settings |= Gadget_Faded;
  129.   }
  130.   else {
  131.     if(!FLAG_SET(flags_settings, Gadget_Faded))
  132.       return NULL;
  133.     flags_settings &= (~Gadget_Faded);
  134.   }
  135.   return gadget_set_flags(0, id, gad, flags_settings);
  136. }
  137.  
  138. /* ----------------------------------------------------------------------- */
  139.  
  140. _kernel_oserror *getscreencentre(int *centreX,int *centreY)
  141. {
  142.   /* Get screen centre */
  143.   _kernel_swi_regs regs;
  144.   int XWindLimit,YWindLimit;
  145.   int XEigFactor,YEigFactor;
  146.  
  147.   regs.r[0]=-1; /* current mode */
  148.   regs.r[1]=11; /* no of x pixels on screen-1 */
  149.   THROW(_kernel_swi(OS_ReadModeVariable,®s,®s));
  150.   XWindLimit=regs.r[2];
  151.  
  152.   regs.r[0]=-1; /* current mode */
  153.   regs.r[1]=12; /* no of y pixels on screen-1 */
  154.   THROW(_kernel_swi(OS_ReadModeVariable,®s,®s));
  155.   YWindLimit=regs.r[2];
  156.  
  157.   regs.r[0]=-1; /* current mode */
  158.   regs.r[1]=4; /* X eigen factor */
  159.   THROW(_kernel_swi(OS_ReadModeVariable,®s,®s));
  160.   XEigFactor=regs.r[2];
  161.  
  162.   regs.r[0]=-1; /* current mode */
  163.   regs.r[1]=5; /* Y eigen factor */
  164.   THROW(_kernel_swi(OS_ReadModeVariable,®s,®s));
  165.   YEigFactor=regs.r[2];
  166.  
  167.   *centreX = (XWindLimit<<XEigFactor)/2;
  168.   *centreY = (YWindLimit<<YEigFactor)/2;
  169.   return NULL;
  170. }
  171.  
  172. /* ----------------------------------------------------------------------- */
  173.  
  174. char *tail(char *pathname, int length)
  175. {
  176.   char *ptr;
  177.   int dotcount;
  178.   
  179.   ptr = (char *)((int)pathname + strlen(pathname)); /* terminator */
  180.   dotcount = 0;
  181.   while(ptr > pathname && dotcount<length) {
  182.     ptr--; /* scan string backwards from terminator */
  183.     if(*ptr == '.')
  184.       dotcount++;
  185.   }
  186.   if(dotcount >= length)
  187.     return (char *)((int)ptr + 1);
  188.   return ptr;
  189. }
  190.  
  191. /* ----------------------------------------------------------------------- */
  192.  
  193. int delete_object_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  194. {
  195.   /* Generic handler for deleting an object */
  196.   RE(toolbox_delete_object(0, id_block->self_id));
  197.  
  198.   return 1; /* claim event */
  199. }
  200.  
  201. /*
  202.  * File loading routines
  203.  */
  204.  
  205. _kernel_oserror *load_compressed(char *filepath, flex_ptr buffer_anchor)
  206. {
  207.   return perc_operation(FILEPERC_OP_DECOMP, filepath, 0, buffer_anchor);
  208. }
  209.  
  210. /* ----------------------------------------------------------------------- */
  211.  
  212. _kernel_oserror *load_plain(char *filepath, flex_ptr buffer_anchor)
  213. {
  214.   return perc_operation(FILEPERC_OP_LOAD, filepath, 0, buffer_anchor);
  215. }
  216.